<?php #20071005 http://www.framework.altervista.org/FrameWork/libs/libCapcha.inc
define('LIBCAPCHA','1.00');

/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/


function rgb_grayscale( $rgb ) {
    $color[ 'r' ] = 0.299 * $rgb[ 'r' ] + 0.587 * $rgb[ 'g' ] + 0.114 * $rgb[ 'b' ];
    $color[ 'g' ] = 0.299 * $rgb[ 'r' ] + 0.587 * $rgb[ 'g' ] + 0.114 * $rgb[ 'b' ];
    $color[ 'b' ] = 0.299 * $rgb[ 'r' ] + 0.587 * $rgb[ 'g' ] + 0.114 * $rgb[ 'b' ];
    return $color;
}

function rgb_complementary( $rgb ) {
    $color[ 'r' ] = 255 - $rgb[ 'r' ];
    $color[ 'g' ] = 255 - $rgb[ 'g' ];
    $color[ 'b' ] = 255 - $rgb[ 'b' ];
    return $color;
}

function rgb_rand( $min=0, $max=255 ) {
    $color[ 'r' ] = rand( $min, $max );
    $color[ 'g' ] = rand( $min, $max );
    $color[ 'b' ] = rand( $min, $max );
    return $color;
}

function rgb_create( $r=0, $g=0, $b=0 ) {
    $color[ 'r' ] = $r;
    $color[ 'g' ] = $g;
    $color[ 'b' ] = $b;
    return $color;
}

function rgb_merge( $lhs, $rhs ) {
    $color[ 'r' ] = ( $lhs[ 'r' ] + $rhs[ 'r' ] ) >> 1;
    $color[ 'g' ] = ( $lhs[ 'g' ] + $rhs[ 'g' ] ) >> 1;
    $color[ 'b' ] = ( $lhs[ 'b' ] + $rhs[ 'b' ] ) >> 1;
    return $color;
}



// crea l'immagine capcha e il file con il codice sicurezza in sb
function capcha_generate(){

    //garbage
    $fd=opendir("datas/capcha/");
    while (false !== ($nf= readdir($fd))){
	if ($nf[0]!='.' )
	    if (filectime( "datas/capcha/$nf") > date-mktime(0,0,0,1,0,0))unlink("datas/capcha/$nf");
    }
    closedir($fd);

    $fd=opendir("datas/sb/".sb_get()."/capcha/");
    while (false !== ($nf= readdir($fd))){
	if ($nf[0]!='.' )
	    if (filectime( "datas/sb/".sb_get()."/capcha/$nf") > date-mktime(0,0,0,1,0,0))unlink("datas/sb/".sb_get()."/capcha/$nf");
    }
    closedir($fd);



    // Configuration
    $CONFIG[ 'font_id' ] = 5;
    $CONFIG[ 'width' ] = 120;
    $CONFIG[ 'height' ] = 30;

    srand( ( double ) microtime() * 1000000 );

    //crea nome e chiave capcha
    $cph_name=rand(0,99999);
    $cph_key=rand(0,99999);

    //salva in sb il file di testo con il capcha
    set_folder("datas/sb/".sb_get()."/capcha");
    $f=fopen("datas/sb/".sb_get()."/capcha/$cph_name","w");
    fwrite($f,$cph_key);
    fclose($f);


    //crea il capcha e lo salva
    set_folder("datas/capcha");

    // Creates a simple image
    $image = imagecreate($CONFIG[ 'width' ],$CONFIG[ 'height' ]);

    // Create random colors
    $rgb = array();
    $rgb[ 'background' ] = rgb_rand( 0, 255 );
    $rgb[ 'foreground' ] = rgb_grayscale( rgb_complementary( $rgb[ 'background' ] ) );

    if ( $rgb[ 'foreground' ][ 'r' ] > 127 ) {
	$inicio = -127;
	$rgb[ 'foreground' ] = rgb_merge( $rgb[ 'foreground' ], rgb_create( 255, 255, 255 ) );
	$rgb[ 'shadow' ] = rgb_merge( rgb_complementary( $rgb[ 'foreground' ] ), rgb_create( 0, 0, 0 ) );
    }
    else{
	$inicio = 0;
	$rgb[ 'foreground' ] = rgb_merge( $rgb[ 'foreground' ], rgb_create( 0, 0, 0 ) );
	$rgb[ 'shadow' ] = rgb_merge( rgb_complementary( $rgb[ 'foreground' ] ), rgb_create( 255, 255, 255 ) );
    }

    // Allocate color resources
    $color = array();
    foreach($rgb as $name => $value) {
	$color[$name] = imagecolorallocate( $image, $value[ 'r' ], $value[ 'g' ], $value[ 'b' ] );
    }

    // Draw background
    imagefilledrectangle( $image, 0, 0,$CONFIG[ 'width' ] ,$CONFIG[ 'height' ] , $color[ 'background' ] );
    // Write some random text on background
    for ( $i = 0; $i < rand( 5, 9 ); $i++ ) {
	$x = rand( 0, $CONFIG[ 'width' ] );
	$y = rand( 0, $CONFIG[ 'height' ] );
	$f = rand( 0, 5 );
	$c = rgb_grayscale( rgb_rand( 127 - $inicio, 254 - $inicio ) );
	$color[$i] = imagecolorallocate( $image, $c[ 'r' ], $c[ 'g' ], $c[ 'b' ] );
	imagestring( $image, $f, $x, $y, $cph_key, $color[$i] );
    }

    // Center the real captcha text
    $x  = ( $CONFIG[ 'width' ] - ( ImageFontWidth($CONFIG[ 'font_id' ] ) * strlen($cph_key) ) ) >> 1;
    $y  = ( $CONFIG[ 'height' ] - ImageFontHeight($CONFIG[ 'font_id' ] ) ) >> 1;

    // Write the captcha text with shadow (improves the human vision in some cases)
    imagestring( $image, $CONFIG[ 'font_id' ], $x + 1, $y + 1, $cph_key, $color[ 'shadow' ] );
    imagestring( $image, $CONFIG[ 'font_id' ], $x, $y, $cph_key, $color[ 'foreground' ] );

    // Save the image
    imagepng($image,"datas/capcha/$cph_name.png" );

    // Free resources
    foreach($color as $name => $value) {
	imagecolordeallocate( $image, $value );
    }
    imagedestroy( $image );

    return $cph_name;
}
//---------------------------------------------------------------------------------------------------------------------

// verifica che il codice di verifica mandato sia corretto e cancella chiave in sb e immagine
function capcha_test($cph_name,$cph_key){
    $cph_realkey=@join(file("datas/sb/".sb_get()."/capcha/$cph_name"));
    if(file_exists("datas/sb/".sb_get()."/capcha/$cph_name"))unlink("datas/sb/".sb_get()."/capcha/$cph_name");
    if(file_exists("datas/capcha/$cph_name.png"))unlink("datas/capcha/$cph_name.png");
    if ($cph_key==$cph_realkey)return true;
    return false;
}
//---------------------------------------------------------------------------------------------------------------------
?>
